﻿using AutoMapper;
using MediatR;
using S.BreedingHistory.Api.Application.Commmand;
using S.BreedingHistory.Domain.Model;
using S.BreedingHistory.Infrastructure.Interface;

namespace S.BreedingHistory.Api.Application.CommmandHandler
{
    public class BreedingHistoryAddCommandHandler : IRequestHandler<BreedingHistoryAddCommand, bool>
    {
        private readonly IBreedingHistoryRepository breedingHistoryRepository;
        private readonly IMapper mapper;

        public BreedingHistoryAddCommandHandler(IBreedingHistoryRepository breedingHistoryRepository,IMapper mapper)
        {
            this.breedingHistoryRepository = breedingHistoryRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 种母历史记录的添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<bool> Handle(BreedingHistoryAddCommand request, CancellationToken cancellationToken)
        {
            BreedingHistoryModel breedingHistoryModel = mapper.Map<BreedingHistoryModel>(request);
            breedingHistoryModel.IsDel=true;
            breedingHistoryModel.IsEditor=true;
            return breedingHistoryRepository.AddAsync(breedingHistoryModel);
        }
    }
}
